package com.ning.parking.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.ning.parking.config.GlobalVariableUtils;
import com.ning.parking.domain.ParkingLotBaseInfo;
import com.ning.parking.domain.ParkingLotBlacklist;
import com.ning.parking.domain.ParkingLotWhitelist;
import com.ning.parking.domain.VO.Pagination;
import com.ning.parking.domain.dto.ParkingLotBlacklistPageDto;
import com.ning.parking.domain.dto.ParkingLotBlacklistSaveUpdateDTO;
import com.ning.parking.domain.dto.ParkingLotWhitelistPageDto;
import com.ning.parking.service.ParkingLotBaseInfoService;
import com.ning.parking.service.ParkingLotBlacklistService;
import com.ning.parking.service.ParkingLotWhitelistService;
import com.scsonic.framework.common.response.R;
import com.scsonic.framework.exception.BizException;
import com.scsonic.framework.util.DateUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author ChenN
 * @Date 2025/4/8 15:41
 * @Version 0.0.1
 **/
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("blacklist")
public class ParkingLotBlacklistController {

    final ParkingLotBlacklistService parkingLotBlacklistService;
    final ParkingLotBaseInfoService parkingLotBaseInfoService;

    /**
     * --------------------------
     * @param parkingLotBlacklist
     * @return
     */
    @PostMapping("saveOrUpdateBlacklist")
    public R<Long> saveOrUpdateBlacklist(@RequestBody ParkingLotBlacklist parkingLotBlacklist) {
        String carLicense = parkingLotBlacklist.getCarLicense();
        if (carLicense == null){
            throw new BizException("carLicense is null");
        }
        List<ParkingLotBlacklist> list = parkingLotBlacklistService.lambdaQuery().eq(ParkingLotBlacklist::getCarLicense, carLicense).eq(ParkingLotBlacklist::getParkingLotId,parkingLotBlacklist.getParkingLotId()).list();

        if (ObjectUtil.isNotEmpty(list)){
            throw new BizException(carLicense+"黑名单已存在记录,请搜索后进行编辑");
        }

        DateTime date = DateUtils.date();
        parkingLotBlacklist.setUpdateTime(date);
        parkingLotBlacklist.setUpdateBy(GlobalVariableUtils.getUser().getUserName());
        parkingLotBlacklist.setCreateBy(GlobalVariableUtils.getUser().getUserName());
        parkingLotBlacklist.setCreateTime(date);
        boolean update = parkingLotBlacklistService.saveOrUpdate(parkingLotBlacklist);

        if (update){
            parkingLotBlacklistService.saveOrUpdateBlacklist(BeanUtil.toBean(parkingLotBlacklist, ParkingLotBlacklistSaveUpdateDTO.class));
        }

        return R.ok(parkingLotBlacklist.getBlacklistId());
    }

    @PostMapping("saveBlacklist")
    public R<Long> saveBlacklist(@RequestBody ParkingLotBlacklist parkingLotBlacklist) {
        String carLicense = parkingLotBlacklist.getCarLicense();
        if (carLicense == null){
            throw new BizException("carLicense is null");
        }
        List<ParkingLotBlacklist> list = parkingLotBlacklistService.lambdaQuery().eq(ParkingLotBlacklist::getCarLicense, carLicense).eq(ParkingLotBlacklist::getParkingLotId,parkingLotBlacklist.getParkingLotId()).list();

        if (ObjectUtil.isNotEmpty(list)){
            throw new BizException(carLicense+"黑名单已存在记录,请搜索后进行编辑");
        }

        DateTime date = DateUtils.date();
        parkingLotBlacklist.setUpdateTime(date);
        parkingLotBlacklist.setUpdateBy(GlobalVariableUtils.getUser().getUserName());
        parkingLotBlacklist.setCreateBy(GlobalVariableUtils.getUser().getUserName());
        parkingLotBlacklist.setCreateTime(date);
        parkingLotBlacklistService.saveOrUpdate(parkingLotBlacklist);
        return R.ok(parkingLotBlacklist.getBlacklistId());
    }

    @PostMapping("updBlacklist")
    public R<Long> updBlacklist(@RequestBody ParkingLotBlacklist parkingLotBlacklist) {
        if (parkingLotBlacklist.getBlacklistId() == null){
            throw new BizException("Blacklist is null ");
        }
        DateTime date = DateUtils.date();
        parkingLotBlacklist.setUpdateTime(date);
        parkingLotBlacklist.setUpdateBy(GlobalVariableUtils.getUser().getUserName());
        parkingLotBlacklistService.updateById(parkingLotBlacklist);
        return R.ok(parkingLotBlacklist.getBlacklistId());
    }

    @GetMapping("get-by-whitelist-id")
    public R<ParkingLotBlacklist> getByWhitelistId( ParkingLotBlacklist parkingLotBlacklist) {
        return R.ok(parkingLotBlacklistService.getById(parkingLotBlacklist.getBlacklistId()));
    }

    @GetMapping("del")
    public R del( ParkingLotBlacklist parkingLotBlacklist) {
        boolean b = parkingLotBlacklistService.removeById(parkingLotBlacklist.getBlacklistId());
        return R.ok(parkingLotBlacklist.getBlacklistId());
    }

    @GetMapping("page")
    public R<Pagination<ParkingLotBlacklist>> page(ParkingLotBlacklistPageDto parkingLotBlacklistPageDto) {
        Pagination<ParkingLotBlacklist> page = parkingLotBlacklistService.selectPage(parkingLotBlacklistPageDto);
        List<ParkingLotBlacklist> records = page.getRecords();
        for (ParkingLotBlacklist record : records) {
            if (record.getParkingLotId()!=null){
                ParkingLotBaseInfo byId = parkingLotBaseInfoService.getById(record.getParkingLotId());
                record.setParkingLotName(byId!=null?byId.getParkingLotName():"--");
                continue;
            }
            record.setParkingLotName("--");
        }
        return R.ok(page);
    }
}
